#ifndef __RESFILE_H__
#define __RESFILE_H__

class CFileStream;
class CMemoryStream;

/// @brief This class provides access to a single resource file packed by "pack" utility.
/// @attention Use CResourceManager::GetResourceAsStream to read resource, avoid using this class directly in game code.
class CResFile
{
public:
	static void LoadFile(void* object, CFileStream *pFS);

	void Initialize(const char * packedFile);
	CResFile();
	~CResFile();
	
	/// @brief Open a resource file.
	///
	/// This resource file must be generated by "pack" utility.
	///	@param[in]	pName	file name of the resource file including path.
	/// @return true if success, or false if failed.
	bool Open(const char *pName);

	/// @brief Close an opened resource file.
	void Close();

	/// @brief Find and open a record by a specified record name.
	/// @attention The name of the resource is case sensitive.
	///	@param[in]	pName	the name of the record.
	/// @return Handle to an opened record, or NULL if failed.
	void* FindRecord(const char *pName);

	/// @brief Get the size of the record data.
	///	@param[in]	hRecord	Handle to the record.
	/// @return Size of the record data.
	unsigned int GetRecordSize(void* hRecord);

	/// @brief Read a whole record into a buffer.
	///	@param[in]	hRecord	Handle to the record.
	///	@param[in]	pBuf	Pointer to a memory buffer to hold the record data.
	///	@param[in]	len	Size of the buffer.
	/// @return The number of record data read in byte, or 0 if the record
	/// size is bigger than the size of the buffer.
	unsigned int ReadRecord(void *hRecord, void *pBuf, unsigned int len);
	
protected:
	class Record
	{
	public:
		unsigned long m_hash;
		unsigned long m_offset;
	};
	
	unsigned int GetRecordPackedSize(void* hRecord);
	unsigned long ComputeHash(const char *s);
	

	int m_recordNumber;
	Record *m_records;
	CMemoryStream *m_file;

private:
	CResFile& operator=(const CResFile& rhs);
public:
	CMemoryStream *GetMemoryStream() {return m_file;};
};

#define PACK_ONCE 0
#define PACK_TEXTS 1
#define PACK_PROFILE 2
#define PACK_COMMON 3
#define PACK_COACHES 4
#define PACK_BOSSMATH 5
#define PACK_BOSSPOLICE 6
#define PACK_BOSSBALLS 7
#define PACK_321 8
#define PACK_CHART 9
#define PACK_DESIGN 10
#define PACK_MINIGAME 11
#define PACK_LOGIC 12
#define PACK_MATH 13
#define PACK_MEMORY 14
#define PACK_VISUAL 15
#define PACK_FOCUS 16
#define PACK_STRESS 17
#define PACK_BONUS 18
#define PACK_BRAIN 19
#define PACK_COUNT 20

extern CResFile _resPack[PACK_COUNT];

#endif
